<?php
class Pagination{
var $totalNumber; //总记录数
var $pagesize; //每页记录数
var $totalPage; //总页数
var $minimum; //从记录集第几条开始取（与数据库有联系）
var $maxpage; //导航页数
var $page; //第几页 $_GET['page']
var $display;


//构造函数 Pagination(总记录数，每页记录数，从记录集第几条开始取，第几页)
function __construct($totalNumber,$pagesize,$page,$maxpage=10){
$this->totalNumber = $totalNumber;
$this->pagesize = $pagesize;
$this->totalPage = $this->totalPageFns();
$this->maxpage = $maxpage;

//在url参数page不存在时，page不为10进制数时，page大于可分页数时，默认为1
if(!isset($page) || !intval($page) || $page > $this->totalPage){
$this->page = 1;
}else{
$this->page = $page;
}

//从记录集第几条开始取（与数据库有联系）
$this->minimum = ($this->page - 1) * $this->pagesize;
$this->fenye();
}

//分页函数直接调用 fenye();
function fenye(){
$page_url_pre = "$filename?" . self::clearQuery($_SERVER['QUERY_STRING'] . "&$extra_url",array('page')) . "&page=";
ob_start();

echo "      <div class='pagination'  ><ul>";
echo	" <li  class='currentpage' >{$this->page}/{$this->totalNumber}</li> ";
echo	" <li class='currentpage' >{$this->page}</li> ";

//实现 << < 1 2 3 4 > >> 分页链接
$pre = $this->page - 1;//上一页
$next = $this->page + 1;//下一页
$maxpages = $this->maxpage;//处理分页时 << < 1 2 3 4 > >>显示4页
$pagepre = 1;//如果当前页面是4，还要显示前$pagepre页，如<< < 3 /4/ 5 6 > >> 把第3页显示出来

if($this->page != 1) { echo "<li class='first' ><a href='{$page_url_pre}1'>&lt;&lt;</a></li>";
echo "<li class='previous' ><a href='{$page_url_pre}$pre'>&lt;</a></li >";}

if($maxpages>=$this->totalPage) //如果总记录不足以显示4页
{$pgstart = 1;$pgend = $this->totalPage;}//就不所有的页面打印处理
elseif(($this->page-$pagepre-1+$maxpages)>$this->totalPage)//就好像总页数是6，当前是5，则要把之前的3 4 显示出来，而不仅仅是4
{$pgstart = $this->totalPage - $maxpages + 1;$pgend = $this->totalPage;}
else{
$pgstart=(($this->page<=$pagepre)?1:($this->page-$pagepre));//当前页面是1时，只会是1 2 3 4 > >>而不会是 0 1 2 3 > >>
$pgend=(($pgstart==1)?$maxpages:($pgstart+$maxpages-1));
}
for($pg=$pgstart;$pg<=$pgend;$pg++){ //跳转菜单<< < 1 2 3 4 > >>
if($pg == $this->page) echo "<li ><a href='{$page_url_pre}$pg'><font color=\"#ff0000\">$pg</font></a> </li >";
else echo "<li ><a href='{$page_url_pre}$pg'>$pg</a> </li >";
}
if($this->page != $this->totalPage)
{echo "<li class='next' ><a href='{$page_url_pre}$next'>&gt;</a> </li >";
echo "<li  class='last' ><a href='{$page_url_pre}{$this->totalPage}'>&gt;&gt;</a> </li >";}

echo "</ul></div>";
	$this->display = ob_get_clean();
}

function display(){
	echo $this->display;
}

//计算总页数
function totalPageFns(){
return ceil($this->totalNumber/$this->pagesize);
}

	static function clearQuery($querystring,$remove_array = array()) {
		$query_string = @explode('&',$querystring);
		foreach($query_string as $v){
			$data = @explode('=',$v);
			if(!isset($data[1])) $data[1] = '';
			$querys[$data[0]] = $data[1];
			unset($data);
		}
		foreach($querys as $k => $v){
			if (in_array($k,$remove_array)) {
				continue;
			}
			if($k && $v) $data[] = "$k=$v";
		}
		Return @implode('&', $data);
	}
	
}
/*
//调用方法：
//我的数据库函数
include_once('../include/sql_fns.php');
//分页类
include_once('pageClass.php');
//建立连接
$link=sql_conn();


$sql="SELECT * FROM `DT_test` order by d_id";
//计算记录总数 $num
$num=mysql_num_rows(sql_query($link,$sql));

//每页记录数 $pagesize
$pagesize=2;

//构造函数 Pagination(总记录数，每页记录数，从记录集第几条开始取，第几页)
$page = new Pagination($num,$pagesize,5,$_GET['page']);

//分页 limit $page->minimum,$pagesize
$sqla="SELECT * FROM `DT_test` limit $page->minimum,$pagesize";
$result=sql_query($link,$sqla);
while($row=mysql_fetch_assoc($result)){
echo $row['d_id'].'<br>';
}
//分页函数
$page->fenye();
*/
?>